﻿/*
 * PROJECT:   LVGL PC Simulator using Visual Studio
 * FILE:      LVGL.Simulator.cpp
 * PURPOSE:   Implementation for LVGL ported to Windows Desktop
 *
 * LICENSE:   The MIT License
 *
 * DEVELOPER: Mouri_Naruto (Mouri_Naruto AT Outlook.com)
 */

#include <Windows.h>

#include "resource.h"

#if _MSC_VER >= 1200
// Disable compilation warnings.
#pragma warning(push)
// nonstandard extension used : bit field types other than int
#pragma warning(disable : 4214)
// 'conversion' conversion from 'type1' to 'type2', possible loss of data
#pragma warning(disable : 4244)
#endif

#include "lua/lauxlib.h"
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lv_drivers/win32drv/win32drv.h"
#include "lvgl/demos/lv_demos.h"
#include "lvgl/examples/lv_examples.h"
#include "lvgl/lvgl.h"
#include "serialport/serial.h"
#include <pthread.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "pthreadVC2.lib")

#if _MSC_VER >= 1200
// Restore compilation warnings.
#pragma warning(pop)
#endif

lua_State *L;
lua_State *globalL = NULL;

lua_State *lua_getstate(void)
{
    return globalL;
}
const char lua_buf[] = {
    "a = arc.scr_act()\n"};

#define F_PATH "bin/brute.bin"
#define LUA_FILNAME "main.lua"
char lua_comp[128];
bool flag = false;

static void lua_user_init(void)
{
    L = luaL_newstate(); /* 建立Lua运行环境 */
    globalL = L;
    luaL_openlibs(L);
    luaL_openlibs_user(L); //加载自己的lib
    luaopen_base(L);

    //luaL_dostring(L, lua_buf);
    luaL_dofile(L, LUA_FILNAME); /*加载lua脚本*/
}

void task_callback()
{
    //int iret = system("Certutil -hashfile main.lua MD5");
    //printf("iret=%d\n", iret);
    //if (iret == -1) printf("%d:%s\n", errno, strerror(errno));
    char cmd[128] = "Certutil -hashfile ";
    strcat(cmd, LUA_FILNAME);
    strcat(cmd, " MD5");
    char pRetMsg[128];
    int msg_len = sizeof(pRetMsg);
    FILE *fp;
    char *p = NULL;
    int res = -1;
    if (cmd == NULL || pRetMsg == NULL || msg_len < 0)
    {
        printf("Param Error!\n");
        return -1;
    }
    if ((fp = _popen(cmd, "r")) == NULL)
    {
        printf("Popen Error!\n");
        return -2;
    }
    else
    {
        memset(pRetMsg, 0, msg_len);
        //get lastest result
        while (fgets(pRetMsg, msg_len, fp) != NULL)
        {
            if ((!strstr(pRetMsg, "CertUtil")) && (!strstr(pRetMsg, "MD5")))
            {
                if (memcmp(lua_comp, pRetMsg, sizeof(pRetMsg)) != 0)
                {
                    memcpy(lua_comp, pRetMsg, sizeof(pRetMsg));
                    //printf("Msg:%s", lua_comp); //print all info
                    if (flag == true)
                    {
                        lua_close(L);
                        L = luaL_newstate(); /* 建立Lua运行环境 */
                        luaL_openlibs(L);
                        luaL_openlibs_user(L); //加载自己的lib
                        luaopen_base(L);
                        luaL_dofile(L, LUA_FILNAME); /*加载lua脚本*/
                    }
                    flag = true;
                }
            }
        }

        if ((res = _pclose(fp)) == -1)
        {
            //printf("close popenerror!\n");
            return -3;
        }
        //pRetMsg[strlen(pRetMsg) - 1] = '\0';
        return 0;
    }
}
PORT COM;
char buff[1024] = {0};
int rcv_len = 0;

void *thread(void *val)
{
    while (1)
    {
        rcv_len = Serial_ReciveData(COM, buff, 1024);
        if (rcv_len > 0)
        {
            luaL_dostring(L, buff);
            //printf("rcv:%s\n", buff);
            memset(buff, 0, 1024);
        }
        Sleep(100);
    }
    return NULL;
}

void *gui_thread(void *val)
{
    while (1)
    {
        Sleep(100);
    }
    return NULL;
}

static void anim_x_cb(void *var, int32_t v)
{
    lv_obj_set_x(var, v);
}

int main()
{
    lv_init();
    pthread_t tid, tid1;

    if (!lv_win32_init(
            GetModuleHandleW(NULL),
            SW_SHOW,
            800,
            480,
            LoadIconW(GetModuleHandleW(NULL), MAKEINTRESOURCE(IDI_LVGL))))
    {
        return -1;
    }

    lv_win32_add_all_input_devices_to_group(NULL);
    lv_png_init();
    lua_user_init();

    COM = serial_init(101, 115200, 8, 1, 0);

    lv_timer_t *task_timer = lv_timer_create(task_callback, 500, NULL);

    pthread_create(&tid, 0, thread, NULL);
    pthread_create(&tid1, 0, gui_thread, NULL);

    lv_example_style_6();
    while (!lv_win32_quit_signal)
    {
        lv_task_handler();
        Sleep(1);
    }
    return 0;
}
